package com.example.mqlearning.producer.controller;

import com.example.mqlearning.producer.config.RabbitMQConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "消息生产者接口")
@RestController
@RequestMapping("/producer")
public class MessageController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ApiOperation(value = "发送消息", notes = "将消息发送到RabbitMQ队列中")
    @GetMapping("/send")
    public String sendMessage(@ApiParam(value = "要发送的消息内容", required = true) @RequestParam String message) {
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, 
                                    RabbitMQConfig.ROUTING_KEY, 
                                    message);
        return "消息发送成功：" + message;
    }
}